package cn.knet.excel.imp.knetexceltool.service;

import cn.knet.pandora.modules.utils.language.punycode.PunycodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.xbill.DNS.*;

import java.net.InetAddress;

@Service
public class DnsService {

    @Value("${dns.bindServer:202.173.9.28}")
    String bindServer = "202.173.9.28";

    public void add(String host, String domain, String value, long ttl, String type, String tld) throws Exception {
        Name name = getName(host, domain, tld);
        Record r = null;
        switch (type) {
            case "A":
                r = new ARecord(name, DClass.IN, ttl, InetAddress.getByName(value));
                break;
            case "AAAA":
                r = new AAAARecord(name, DClass.IN, ttl, InetAddress.getByName(value));
                break;
            case "CNAME":
                r = new CNAMERecord(name, DClass.IN, ttl, getName(value));
                break;
            case "NS":
                r = new NSRecord(name, DClass.IN, ttl, getName(value));
                break;
//            case "MX":
//                r = new MXRecord(name, 1, ttl, mx, getName(value));
            //  break;
            case "TXT":
                r = new TXTRecord(name, DClass.IN, ttl, value);
                break;
            default:
                throw new RuntimeException("解析类型不正确");
        }
        Update update = new Update(getName(tld));
        update.add(r);
        update(update);

    }

    public void remove(String host, String domain, String tld) throws Exception {
        Update update = new Update(getName(tld));
        update.delete(getName(host, domain, tld));
        update(update);
    }


    private Name getName(String value) throws TextParseException {
        if (!value.endsWith(".")) {
            value += ".";
        }
        return Name.fromString(PunycodeUtil.chinese2punycode(value));
    }

    private Name getName(String host, String domain, String tld) throws TextParseException {
        String value = PunycodeUtil.chinese2punycode(domain + "." + tld + ".");
        if (StringUtils.isNotBlank(host) && !"@".equals(host)) {
            if ("*".equals(host)) {
                value = "*." + value;
            } else if (host.startsWith("*.")) {
                value = "*." + PunycodeUtil.chinese2punycode(host.substring(2)) + "." + value;
            } else {
                //if (host.contains("@")) host = host.replaceAll("@", "---------");
                value = PunycodeUtil.chinese2punycode(host) + "." + value;
                //if (host.contains("---------")) host = host.replaceAll("---------", "@");
            }
        }
        return Name.fromString(value);
    }


    protected void update(Update update) throws Exception {
        Resolver resolver = new SimpleResolver(bindServer);
        resolver.setPort(53);
        resolver.setTCP(true);
        Message response = resolver.send(update);
    }


    public static void main(String[] args) throws Exception {
        DnsService dns = new DnsService();
        dns.add("_@", "哈1哈1哈", "abc.com", 900, "TXT", "网址");
//        dns.add("我是abc", "哈1哈1哈", "202.173.9.28", 900, "A", "网址");
//        dns.add("def", "哈1哈1哈", "202.173.9.28", 900, "A", "网址");
//        dns.add("我好人啊", "哈1哈1哈", "202.173.9.28", 900, "A", "网址");
//
//
//        dns.remove("a", "哈1哈1哈", "网址");


    }


}
